package com.tommy.store.http;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.util.EntityUtils;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;

public class MyHttpGet {
    public static void main(String[] args) {

    }

    public static ResponseEntity<InputStreamResource> downloadFile(String url, String name) throws IOException {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {

            // 创建连接管理器
            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
            // 设置最大连接数
            cm.setMaxTotal(1);
            // 设置每个主机的最大连接数
            cm.setDefaultMaxPerRoute(1);

            // 创建CloseableHttpClient实例，并使用自定义配置
            httpClient = HttpClients.custom()
                    .setConnectionManager(cm)
                    .build();

            // CloseableHttpClient httpClient = HttpClients.createDefault();

            HttpGet request = new HttpGet(url);
            response = httpClient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream inputStream = entity.getContent();
                InputStreamResource resource = new InputStreamResource(inputStream);

                // Set headers
                // HttpHeaders headers = new HttpHeaders();
                // headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;
                // filename=downloadedfile");
                // MediaType mediaType = MediaType.parseMediaType("application/octet-stream");

                return ResponseEntity.ok()
                        .header("Content-Disposition", "attachment; filename=\"" + name + "\"")
                        // .contentLength(entity.getContentLength())
                        // .contentType(mediaType)
                        .body(resource);
            } else {
                throw new IOException("No entity found in response");
            }

        } catch (Exception e) {
            // TODO: handle exception
            return null;
        } finally {
            httpClient.close();
            response.close();
        }
    }

    public static HttpEntity request(String url, String fileName, String content) {

        // String urlBase = "https://openapi.alipan.com";

        HttpEntity result = null;
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(url); // 目标URL

            // 执行请求
            HttpResponse response = httpClient.execute(httpGet);

            // 打印响应状态
            System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

            // 打印响应内容
            result = response.getEntity();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
